package study.zhangbo.lock.distributed.redis;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhangbo on 2020/10/29.
 */
@Slf4j
public class RedisLockDeamon implements Runnable {


    private RedisLockUtils redisLockUtils;


    public RedisLockDeamon(RedisLockUtils redisLockUtils) {
        this.redisLockUtils = redisLockUtils;
    }


    @Override
    public void run() {
        while (true) {
            List<String> locks = RedisLock.lockDaemon();
            if(locks.isEmpty()) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    log.error("守护线程收到异常中断标志：{}",e);
                }
            }
            for (String lock : locks) {
                if(!redisLockUtils.hasKey(lock)) {
                    RedisLock.awakenThread(lock);
                }
            }
        }
    }

}
